/* 

  FITS related utility


  Copyright © 2011 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.


*/

#include "fitsut.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  int mode = ID_NULL;
  string filename;
  vector<string> keywords;
  int keylist = ID_FULL;

  while( cin.good() ) {

    string line;
    getline(cin,line);

    size_t eq = line.find('=');

    string value;
    if( eq != string::npos ) 
      value = line.substr(eq+1);

    if( line.find("MODE") != string::npos && eq != string::npos ) {
    
      if( value.find("STRUCTURE") != string::npos )
	mode = ID_STRUCTURE;
      else if ( value.find("HEADER") != string::npos )
	mode = ID_HEADER;
      else if ( value.find("TABLE") != string::npos )
	mode = ID_TABLE;
      else if ( value.find("IMAGE") != string::npos )
	mode = ID_IMAGE;

    }

    if( line.find("KEYWORD") != string::npos && eq != string::npos ) {

      size_t i = value.find('\'');
      size_t j = value.rfind('\'');
      size_t n = j - i - 1;
      keywords.push_back(value.substr(i+1,n));

    }

    if( line.find("KEYLIST") != string::npos && eq != string::npos )
      keylist = ID_SHORT;

    if( line.find("FILE") != string::npos && eq != string::npos ) {

      size_t i = value.find('\'');
      size_t j = value.rfind('\'');
      size_t n = j - i - 1;
      filename = value.substr(i+1,n);

    }

  }
  

  if( mode == ID_STRUCTURE )
    return structure(filename);

  else if( mode == ID_HEADER )
    return header(filename,keywords,keylist);

  else if( mode == ID_TABLE )
    return table(filename);

  else if( mode == ID_IMAGE )
    return image(filename);

  return 1;
}
